// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import store from './vuex/store'
import { AjaxPlugin, LoadingPlugin, ToastPlugin,ConfirmPlugin } from 'vux'
import VueScroller from 'vue-scroller'
import App from './App'
import router from './router'
//vux插件
Vue.use(AjaxPlugin)
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)
Vue.use(ConfirmPlugin)
Vue.use(VueScroller)
//请求拦截判断登录状态是否过期
const getToken = () => {
  let token = ''
  if(localStorage.getItem("userData")){
    token = JSON.parse(localStorage.getItem("userData")).auth_token
  }
  return token
}
AjaxPlugin.$http.interceptors.request.use(config => {
  //添加token
  if (config.method == 'post' && !/^\/api\/com\.upload/.test(config.url)) {
    config.data = {
      ...config.data,
      auth_token: getToken()
    }
  }
  if (config.method == 'get') {
    config.params = {
      ...config.params,
      auth_token: getToken()
    }
  }
  Vue.$vux.loading.show()
  return config
})
AjaxPlugin.$http.interceptors.response.use(res => {
  //未登录跳转登录页面
  if (res.data.code == '-6') {
    localStorage.clear()
      Vue.$vux.toast.show({
        text:res.data.msg,
        type:'warn'
      })
      router.replace({ name: 'login' })
  }
  Vue.$vux.loading.hide()
  return res
})
//vuex
store.registerModule('vux', {
  state: {
    direction: 'forward'
  },
  mutations: {
    updateDirection(state, payload) {
      state.direction = payload.direction
    }
  }
})
//订单数据
store.registerModule('order', {
  state: {
    num: 0
  },
  mutations: {
    changeNum(state, num) {
      state.num = num
    }
  }
})
//路由状态
store.registerModule('router', {
  state: {
    router_goods_name: '/index/goods'
  },
  mutations: {
    changerouter(state, data) {
      state[data.currouter] = data.changerouter
    }
  }
})
//设置过渡动画
const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)
let isPush = false
let endTime = Date.now()
let methods = ['push', 'go', 'replace', 'forward', 'back']

document.addEventListener('touchend', () => {
  endTime = Date.now()
})
methods.forEach(key => {
  let method = router[key].bind(router)
  router[key] = function (...args) {
    isPush = true
    method.apply(null, args)
  }
})
router.beforeEach(function (to, from, next) {
  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)
  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('updateDirection', { direction: 'forward' })
    } else {
      // 判断是否是ios左滑返回
      if (!isPush && (Date.now() - endTime) < 377) {
        store.commit('updateDirection', { direction: '' })
      } else {
        store.commit('updateDirection', { direction: 'reverse' })
      }
    }
  } else {
    ++historyCount
    history.setItem('count', historyCount)
    to.path !== '/' && history.setItem(to.path, historyCount)
    store.commit('updateDirection', { direction: 'forward' })
  }

  if (/\/http/.test(to.path)) {
    let url = to.path.split('http')[1]
    window.location.href = `http${url}`
  } else {
    next()
  }
})
//动态设置标题
router.afterEach(route => {
  isPush = false
  if (route.meta.title) {
    document.title = route.meta.title;
  }
})

/* eslint-disable no-new */
Vue.config.productionTip = false
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
